package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.zl.smartmall.library.po.AdvertInfopic;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

/**
 *  闪屏页广告加载成功
 *
 */
public abstract class AdvertiseListener extends BaseListener {

	private static final String TAG = AdvertiseListener.class.getSimpleName();

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		
			try {
				int status = response.getInt(Constants.REQ_STATUS);
				String msg = response.getString(Constants.REQ_MSG);
				CrashHandler.d(TAG, "商城活动(首)页服务器status = " + status);
				CrashHandler.d(TAG, "商城活动(首)页服务器msg = " + msg);
				
				switch(status){
				case Constants.REQ_OK:
					JSONObject jsonObject = response.getJSONObject(Constants.REQ_RESULT);
					JSONArray listArray = jsonObject.getJSONArray("list");
					List<AdvertInfopic> advertlist = new ArrayList<AdvertInfopic>();
					for (int i = 0; i < listArray.length(); i++) {
						JSONObject advertise = listArray.getJSONObject(i);
						AdvertInfopic advert = new AdvertInfopic();
						advert.setPicture(jsonObject.getString("images_host")+advertise.getString("ad_pic"));
						advertlist.add(advert);
					}
					onResponseSuccess(advertlist,jsonObject.toString());
					break;
				
				case Constants.REQ_OFFLINE:
					onOffline();
					break;
					
				case Constants.REQ_NO_MORE_DATA:
					onNoMoreData();
					break;
					
				default:
					onResponseFailed();
				}
				
			} catch (JSONException e) {
				onFailure(statusCode, headers, e, new JSONObject());
			}
	}
	
	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
		Log.i(TAG, "请求失败");
	}
	
	/**
	 * 闪屏页广告加载成功
	 
	 */
	public abstract void onResponseSuccess(List<AdvertInfopic> advertInfos,String json);
	
}
